package com.lc.app.myaccount;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lc.app.myaccount.activity.Main;
import com.lc.app.myaccount.dao.UserDao;
import com.lc.app.myaccount.entity.UserEntity;
import com.lc.app.myaccount.helper.DBOpenHelper;

public class MainActivity extends AppCompatActivity {

    private Button btnLogin;
    private Button btnClose;
    private EditText txtUserName;
    private EditText txtPassword;

    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtUserName = (EditText) findViewById(R.id.txtUserName);
        txtPassword = (EditText) findViewById(R.id.txtPassword);

        userDao = new UserDao(getApplicationContext());

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnClose = (Button) findViewById(R.id.btnClose);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strUserName = txtUserName.getText().toString();
                String strPwd = txtPassword.getText().toString();
                if(strUserName == null || strPwd == null || "".equals(strUserName) || "".equals(strPwd)){
                    Toast.makeText(getApplicationContext(),"账号或者密码不能为空",Toast.LENGTH_SHORT).show();
                }else{
                    UserEntity user = userDao.findByName(strUserName);
                    if(user!= null && user.getPassword().equals(strPwd)){
                        Intent intent = new Intent(MainActivity.this, Main.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(getApplicationContext(),"密码输入错误",Toast.LENGTH_SHORT).show();
                    }
                }
                txtPassword.setText("");
            }
        });


        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });




    }

}
